﻿// Copyright (c) 2010
// by http://openlightgroup.net/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.VisualBasic;
using DotNetNuke.Security.Roles;

namespace SilverlightDebateForum
{
    public class RIAAuthentication
    {
        private DotNetNuke.Entities.Users.UserInfo DNNUserInfo =
            new DotNetNuke.Entities.Users.UserInfo();

        private int _PortalID;
        private int _UserID;
        private string _Password;
        private int _ModuleId;
        private string _IPAddress;

        public RIAAuthentication()
        {
            _PortalID = 0;
            _ModuleId = 0;
            _UserID = -1;
            _Password = "";
            _IPAddress = "127.0.0.1";
        }

        public RIAAuthentication(RIAAuthenticationHeader Credentials)
        {
            _PortalID = Credentials.PortalID;
            _ModuleId = Credentials.ModuleId;
            _UserID = Credentials.UserID;
            _IPAddress = Credentials.IPAddress;

            // Get a the Hashed password
            Utility Utility = new Utility();
            string strHashPassword = Utility.HashPassword(Credentials.UserID, Credentials.Password.Trim());
            // Set the Password
            _Password = strHashPassword;
        }

        #region SetRIAKey
        public static string SetRIAKey(int intUserID, string IPAddress)
        {
            // Get a random password
            Utility Utility = new Utility();
            string strRandomPassword = Utility.GetRandomPassword();

            if (intUserID > 0)
            {
                string strHashPassword = "";
                strHashPassword = Utility.HashPassword(intUserID, strRandomPassword);

                SilverlightDebateForumDBDataContext db = new SilverlightDebateForumDBDataContext();

                var ADefHelpDeskRIAUser = (from RIAUser in db.SilverDebate_RIAUsers
                                           where RIAUser.UserID == intUserID
                                           select RIAUser).FirstOrDefault();

                if (ADefHelpDeskRIAUser == null)
                {
                    // New RIA User
                    SilverDebate_RIAUser RIAUser = new SilverDebate_RIAUser();

                    RIAUser.UserID = intUserID;
                    RIAUser.RIAPassword = strHashPassword;
                    RIAUser.IPAddress = IPAddress;
                    RIAUser.CreatedDate = DateAndTime.Now;

                    db.SilverDebate_RIAUsers.InsertOnSubmit(RIAUser);
                }
                else
                {
                    // Update RIA User
                    ADefHelpDeskRIAUser.RIAPassword = strHashPassword;
                    ADefHelpDeskRIAUser.IPAddress = IPAddress;
                    ADefHelpDeskRIAUser.CreatedDate = DateAndTime.Now;
                }

                db.SubmitChanges();
            }

            return strRandomPassword;
        }

        public string SetRIAKey()
        {
            return SetRIAKey(_UserID, _IPAddress);
        }
        #endregion

        #region IsUserValid
        public bool IsUserValid()
        {
            bool UserValid = false;

            if (_UserID == -1)
            {
                return UserValid;
            }

            SilverlightDebateForumDBDataContext db = new SilverlightDebateForumDBDataContext();

            //  Get the SilverlightKey
            var SilverlightUser = (from SilverDebate_RIAUsers in db.SilverDebate_RIAUsers
                                   where SilverDebate_RIAUsers.UserID == _UserID
                                   select SilverDebate_RIAUsers).FirstOrDefault();


            if ((SilverlightUser.RIAPassword == _Password) & (Strings.Left(SilverlightUser.IPAddress, 6) == Strings.Left(_IPAddress, 6)))
            {
                UserValid = true;
            }
            else
            {
                if (Strings.Left(SilverlightUser.IPAddress, 6) == Strings.Left(_IPAddress, 6))
                {
                    // The correct IP address was used
                    // To prevent a brute force attack scramble the password
                    // A hacker is now chasing a moving target
                    SetRIAKey();
                }
                return false;
            }

            return UserValid;
        }
        #endregion

        #region GetUserInfo
        public UserInfo GetUserInfo()
        {
            UserInfo objUserInfo = new UserInfo();
            DNNUserInfo = DotNetNuke.Entities.Users.UserController.GetUser(_PortalID, _UserID, false);

            if (DNNUserInfo != null)
            {
                Utility.ConvertDNNUserToSilverlightDebateForumUser(ref objUserInfo, DNNUserInfo);
            }

            return objUserInfo;
        }

        public UserInfo GetUserInfo(int PortalID, int UserID)
        {
            UserInfo objUserInfo = new UserInfo();

            if (UserID == -1)
            {
                DotNetNuke.Entities.Users.UserInfo DNNUserInfo = new DotNetNuke.Entities.Users.UserInfo();
                DNNUserInfo = DotNetNuke.Entities.Users.UserController.GetUser(PortalID, UserID, false);

                if (DNNUserInfo != null)
                {
                    Utility.ConvertDNNUserToSilverlightDebateForumUser(ref objUserInfo, DNNUserInfo);
                }
            }

            return objUserInfo;
        }
        #endregion
    }
}